package controllers

import (
	"encoding/json"
	"gitchina/kly_service/models"
	"gitchina/kly_service/util"

	"github.com/astaxie/beego"
)

type PointController struct {
	beego.Controller
}

var SECRET_KEY = "**@#PP1234<<"

// @router /106001 [post]
func (m *MemberController) AddPointForAuthCompletion() {
	var rq models.PointAdd
	// if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
	// 	val := fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("%s%s", rq.Rand, SECRET_KEY))))
	// 	if val == rq.Token {
	// 		models.AddPointForAuthCompletion(rq.ID)
	// 	} else {
	// 		m.Ctx.ResponseWriter.WriteHeader(403)
	// 	}
	// } else {
	// 	m.Ctx.ResponseWriter.WriteHeader(400)
	// }

	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		models.AddPointForAuthCompletion(rq.MemberID)
		m.Data["json"] = models.NewOkResponse(nil)
		m.ServeJSON()
	} else {
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}

// @router /106002 [post]
func (m *MemberController) AddPointForRecommendation() {
	var rq models.PointAdd
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &rq); err == nil {
		models.AddPointForRecommendation(rq.MemberID)
		m.Data["json"] = models.NewOkResponse(nil)
		m.ServeJSON()
	} else {
		util.GetLogger().Error(err.Error())
		m.Ctx.ResponseWriter.WriteHeader(400)
	}
}
